#!/usr/bin/python3
# -*- coding: utf-8 -*-
#======================================#
# @Author   : duanqizhong
# @time     : 2022-8-16 10:51
# @file     : func
# @Software : PyCharm
#======================================#
import os
import sys

import yaml
from settings import BASE_DIR
from base.logger import log
from base.cls import ParseConf,Mysql

def get_yaml_dict(yaml_path):
    assert os.path.isfile(yaml_path), f"not fount yaml_path: {yaml_path}"
    fr = open(file=yaml_path, mode='r', encoding='utf-8')
    yaml_json = yaml.load(stream=fr, Loader=yaml.FullLoader)
    if "handlers" in yaml_json.keys():
        # log.yaml
        for key, value in yaml_json['handlers'].items():
            if "filename" in value:
                value['filename'] = os.path.join(BASE_DIR, value["filename"]).replace("\\", '/')
    return yaml_json

def parse_conf(conf_path):
    assert os.path.exists(conf_path),log.error(f"file not exists: {conf_path}")
    return ParseConf(conf_path)

def slow_log_path(conn):
    sql="""show variables like 'slow_query_log_file'"""
    # variable_name,value
    op=Mysql(conn)
    op.connect(None)
    infos=op.fetchone(sql)
    return infos
